package com.example.demo.controller;

import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/test")
    public String test(){

        return "stu test";
    }

    //RESTfaul 风格推荐的写法
    @GetMapping("/{id}")
    public Student getById(@PathVariable("id") Long id){
        return  studentService.getStudentById(id);
    }

    @GetMapping("/getById")
    public Student getById2(@Param("id") Long id){
        return  studentService.getStudentById(id);
    }

    @GetMapping("/list")
    public List<Student> list(){
        return studentService.list();
    }

    @PostMapping("/")
    public String insert(@RequestBody Student student){
        int ret = studentService.insert(student);
        return ret == 1 ? "success" : "fail";
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable("id") Long id){
        int ret = studentService.deleteById(id);
        return ret == 1 ? "success" : "fail";
    }

    @PutMapping("/{id}")
    public String updateWithId(@PathVariable("id") Long id,@RequestBody Student student){
        int ret = studentService.updateWithId(id,student);
        return ret == 1 ? "success" : "fail";
    }
    @PutMapping("/")
    public String update(@RequestBody Student student){
        int ret = studentService.update(student);
        return ret == 1 ? "success" : "fail";
    }
}
